﻿Imports System.Threading
Imports WMPLib
Public Class FormMain

#Region "Moving Form Sub"
    ''Subroutine for moving the form
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case (m.Msg)
            Case WM_NCHITTEST
                MyBase.WndProc(m)
                If (m.Result.ToInt32 = HTCLIENT) Then
                    m.Result = New IntPtr(HTCAPTION)
                End If
            Case WM_NCLBUTTONDBLCLK
                m.Msg = 0 ' Discard double-click
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub
#End Region

#Region "Track Text"
    Private threadstatus As Thread
    Private Sub TrackInfo()
        Do
            Track.Possition = wmp.Ctlcontrols.currentPositionString
            If wmp.playState = 3 Or wmp.playState = 9 Then DisplayInfo()
            If wmp.playState = 3 Then
                pct04Play.Image = My.Resources.blue_04glow
            ElseIf pct04Play.Enabled = False Then
                pct04Play.Image = My.Resources.blue_04nouse
            Else
                pct04Play.Image = My.Resources.blue_04norm
            End If
            Thread.Sleep(1000)
        Loop
    End Sub
    Public Sub DisplayInfo()

        Track.Artist = wmp.Ctlcontrols.currentItem.getItemInfo("Artist")
        Track.Title = wmp.Ctlcontrols.currentItem.getItemInfo("Title")
        Track.Duration = wmp.currentMedia.durationString
        CreateDisplayString()
        SetTrackText(Track.Artist.Trim + vbNewLine + Track.Title.Trim)
        DisplayTrack(DisplayString)
    End Sub
    Public Sub SetTrackText(ByVal [text] As String)
        ' InvokeRequired required compares the thread ID of the
        ' calling thread to the thread ID of the creating thread.
        ' If these threads are different, it returns true.
        If lblTrackInfo.InvokeRequired Then
            Dim d As New ContextCallback(AddressOf SetTrackText)
            Me.Invoke(d, New Object() {[text]})
        Else
            Me.lblTrackInfo.Text = [text]
        End If
    End Sub
#End Region

#Region "Open/Close Form"

    Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        End
    End Sub

    Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(194, 63)
        Me.TopMost = My.Settings.Topmost
        threadstatus = New Thread(New ThreadStart(AddressOf TrackInfo))
        threadstatus.IsBackground = True
        threadstatus.Start()
        UpdatePlayer()
        wmp.settings.volume = My.Settings.PlayerVolume
            WinInit.CreateHandlers()
            If My.Settings.WindowerDisplay = "single" Then
                DisplayTrack("FFXI Media Player Loaded: Please load a playlist")
            ElseIf My.Settings.WindowerDisplay = "multi" Then
                DisplayTrack("FFXI Media Player Loaded" + vbNewLine + "Please load a playlist")
        End If
        pct01Back.Enabled = False
        pct02Stop.Enabled = False
        pct03Pause.Enabled = False
        pct04Play.Enabled = False
        pct05Next.Enabled = False
    End Sub
#End Region

#Region "Player Controls"

    Private Sub pct01Back_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct01Back.Click
        PlayerControl("back", "")
    End Sub

    Private Sub pct02Stop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct02Stop.Click
        PlayerControl("stop", "")
        threadstatus.Abort()

    End Sub

    Private Sub pct03Pause_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct03Pause.Click
        PlayerControl("pause", "")

    End Sub

    Private Sub pct04Play_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct04Play.Click
        PlayerControl("play", "")
        If threadstatus.ThreadState = ThreadState.Aborted Then
            threadstatus = New Thread(New ThreadStart(AddressOf TrackInfo))
            threadstatus.Start()
        End If
    End Sub

    Private Sub pct05Next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct05Next.Click
        PlayerControl("next", "")
    End Sub

    Private Sub pct06File_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct06File.Click
        If e.Button = Windows.Forms.MouseButtons.Left Then
            mnuFile.Show(CType(sender, Control), e.Location)
        End If
    End Sub

    Private Sub pct07Volume_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct07Volume.Click
        FormVolume.ShowDialog()
    End Sub


    Private Sub pct08Power_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct08Power.Click
        RemDisplayTrack()
        My.Settings.Save()
        Me.Close()
    End Sub
#End Region

#Region "Player Controls Images"
    ''Image switches for back button
#Region "Back Button"

    Private Sub pct01Back_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct01Back.EnabledChanged
        If pct01Back.Enabled = True Then
            GUIImage("back", "enable")
        Else
            GUIImage("back", "disable")
        End If
    End Sub

    Private Sub pct01Back_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct01Back.MouseDown
        GUIImage("back", "mdown")
    End Sub

    Private Sub pct01Back_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct01Back.MouseHover
        GUIImage("back", "hover")
    End Sub

    Private Sub pct01Back_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct01Back.MouseLeave
        GUIImage("back", "leave")
    End Sub

    Private Sub pct01Back_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct01Back.MouseUp
        GUIImage("back", "mup")
    End Sub
#End Region

    ''Image Switches for Stop button
#Region "Stop Button"

    Private Sub pct02Stop_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct02Stop.EnabledChanged
        If pct02Stop.Enabled = True Then
            GUIImage("stop", "enable")
        Else
            GUIImage("stop", "disable")
        End If
    End Sub

    Private Sub pct02Stop_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct02Stop.MouseDown
        GUIImage("stop", "mdown")
    End Sub

    Private Sub pct02Stop_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct02Stop.MouseHover
        GUIImage("stop", "hover")
    End Sub

    Private Sub pct02Stop_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct02Stop.MouseLeave
        GUIImage("stop", "leave")
    End Sub

    Private Sub pct02Stop_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct02Stop.MouseUp
        GUIImage("stop", "mup")
    End Sub
#End Region

    ''Image Switches for Pause button
#Region "Pause Button"

    Private Sub pct03Pause_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct03Pause.EnabledChanged
        If pct03Pause.Enabled = True Then
            GUIImage("pause", "enable")
        Else
            GUIImage("pause", "disable")
        End If
    End Sub

    Private Sub pct03Pause_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct03Pause.MouseDown
        GUIImage("pause", "mdown")
    End Sub

    Private Sub pct03Pause_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct03Pause.MouseHover
        GUIImage("pause", "hover")
    End Sub

    Private Sub pct03Pause_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct03Pause.MouseLeave
        GUIImage("pause", "leave")
    End Sub

    Private Sub pct03Pause_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct03Pause.MouseUp
        GUIImage("pause", "mup")
    End Sub
#End Region

    ''Image Switches for Play button
#Region "Play Button"

    Private Sub pct04Play_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct04Play.EnabledChanged
        If pct04Play.Enabled = True Then
            GUIImage("play", "enable")
        Else
            GUIImage("play", "disable")
        End If
    End Sub

    Private Sub pct04Play_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct04Play.MouseDown
        GUIImage("play", "mdown")
    End Sub

    Private Sub pct04Play_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct04Play.MouseHover
        GUIImage("play", "hover")
    End Sub

    Private Sub pct04Play_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct04Play.MouseLeave
        GUIImage("play", "leave")
    End Sub

    Private Sub pct04Play_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct04Play.MouseUp
        GUIImage("play", "mup")
    End Sub
#End Region

    ''Image Switches for Next button
#Region "Next Button"

    Private Sub pct05Next_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct05Next.EnabledChanged
        If pct05Next.Enabled = True Then
            GUIImage("next", "enable")
        Else
            GUIImage("next", "disable")
        End If
    End Sub

    Private Sub pct05Next_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct05Next.MouseDown
        GUIImage("next", "mdown")
    End Sub

    Private Sub pct05Next_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct05Next.MouseHover
        GUIImage("next", "hover")
    End Sub

    Private Sub pct05Next_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct05Next.MouseLeave
        GUIImage("next", "leave")
    End Sub

    Private Sub pct05Next_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct05Next.MouseUp
        GUIImage("next", "mup")
    End Sub
#End Region

    ''Image Switches for File button
#Region "File Button"

    Private Sub pct06File_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct06File.EnabledChanged
        If pct06File.Enabled = True Then
            GUIImage("file", "enable")
        Else
            GUIImage("file", "disable")
        End If
    End Sub

    Private Sub pct06File_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct06File.MouseDown
        GUIImage("file", "mdown")
    End Sub

    Private Sub pct06File_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct06File.MouseHover
        GUIImage("file", "hover")
    End Sub

    Private Sub pct06File_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct06File.MouseLeave
        GUIImage("file", "leave")
    End Sub

    Private Sub pct06File_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct06File.MouseUp
        GUIImage("file", "mup")
    End Sub
#End Region

    ''Image Switches for Power button
#Region "Power Button"

    Private Sub pct08Power_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct08Power.EnabledChanged
        If pct08Power.Enabled = True Then
            GUIImage("power", "enable")
        Else
            GUIImage("power", "disable")
        End If
    End Sub

    Private Sub pct08Power_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct08Power.MouseDown
        GUIImage("power", "mdown")
    End Sub

    Private Sub pct08Power_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct08Power.MouseHover
        GUIImage("power", "hover")
    End Sub

    Private Sub pct08Power_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pct08Power.MouseLeave
        GUIImage("power", "leave")
    End Sub

    Private Sub pct08Power_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pct08Power.MouseUp
        GUIImage("power", "mup")
    End Sub
#End Region

#End Region

#Region "Volue/Mute"
    Public Sub Vol(cVol As Integer)
        wmp.settings.volume = cVol
    End Sub
    Public Sub Mute(cMute As Boolean)
        wmp.settings.mute = cMute
    End Sub
#End Region

    Private Sub OpenPlaylistToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenPlaylistToolStripMenuItem.Click
        PlayerControl("file", "playlist")
    End Sub


    Private Sub SettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        FormSettings.ShowDialog()
    End Sub

    Public Sub UpdatePlayer()
        wmp.settings.setMode("shuffle", My.Settings.PlayerRandom)
        wmp.settings.setMode("loop", My.Settings.PlayerLoop)
    End Sub
End Class
